import Dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import duration from 'dayjs/plugin/duration';
import quarterOfYear from 'dayjs/plugin/quarterOfYear';
import relativeTime from 'dayjs/plugin/relativeTime';
import utc from 'dayjs/plugin/utc';
import { Nullable } from './types';

Dayjs.locale('zh-cn');
Dayjs.extend(relativeTime);
Dayjs.extend(duration);
Dayjs.extend(utc);
Dayjs.extend(quarterOfYear);

export const dayjs = Dayjs;

/**
 * 格式化时间, 默认 YYYY-MM-DD HH:mm:ss
 */
export const formatDateTime = (datetime?: Nullable<string | number>, format = 'YYYY-MM-DD HH:mm:ss') => {
  if (!datetime) {
    return '';
  }
  return dayjs(datetime).format(format);
};
